﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class ThayDoiChucVuReduceBO : Csla.BusinessBase<ThayDoiChucVuReduceBO>
    {
        #region Business Properties

        private int? _ThayDoiChucVuID = null;
        public int? ThayDoiChucVuID
        {
            get { return this._ThayDoiChucVuID; }
            set { this._ThayDoiChucVuID = value; PropertyHasChanged("ThayDoiChucVuID"); }
        }

        private int? _NhanVienID = Session.NhanVienID;
        public int? NhanVienID
        {
            get { return this._NhanVienID; }
            set { this._NhanVienID = value; PropertyHasChanged("NhanVienID"); }
        }

        private int? _PhongBanID = null;
        public int? PhongBanID
        {
            get { return this._PhongBanID; }
            set { this._PhongBanID = value; PropertyHasChanged("PhongBanID"); }
        }

        private int? _ChucVuID = Session.MacDinh_ChucVu;
        public int? ChucVuID
        {
            get { return this._ChucVuID; }
            set { this._ChucVuID = value; PropertyHasChanged("ChucVuID"); }
        }

        private DateTime? _NgayApDung = DateTime.Now;
        public DateTime? NgayApDung
        {
            get { return this._NgayApDung; }
            set { this._NgayApDung = value; PropertyHasChanged("NgayApDung"); }
        }

        protected override object GetIdValue()
        {
            return _ThayDoiChucVuID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newThayDoiChucVuID = -1;
        private ThayDoiChucVuReduceBO()
        {
            _ThayDoiChucVuID = newThayDoiChucVuID--;
        }

        public ThayDoiChucVuReduceBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public ThayDoiChucVuReduceBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static ThayDoiChucVuReduceBO NewRootThayDoiChucVuReduceBO()
        {
            return DataPortal.Create<ThayDoiChucVuReduceBO>(new RootCriteria());
        }

        internal static ThayDoiChucVuReduceBO NewChildThayDoiChucVuReduceBO()
        {
            return DataPortal.Create<ThayDoiChucVuReduceBO>(new ChildCriteria());
        }

        public static ThayDoiChucVuReduceBO GetRootThayDoiChucVuReduceBO(int _ThayDoiChucVuID)
        {
            return DataPortal.Fetch<ThayDoiChucVuReduceBO>(new RootCriteria(_ThayDoiChucVuID));
        }

        public static ThayDoiChucVuReduceBO GetRootThayDoiChucVuReduceBOByWhere(string where)
        {
            return DataPortal.Fetch<ThayDoiChucVuReduceBO>(where);
        }

        internal static ThayDoiChucVuReduceBO GetChildThayDoiChucVuReduceBO(SqlDataReader dataReader)
        {
            return new ThayDoiChucVuReduceBO(dataReader);
        }

        public static void DeleteThayDoiChucVuReduceBO(int _ThayDoiChucVuID)
        {
            DataPortal.Delete(new RootCriteria(_ThayDoiChucVuID));
        }

        public static void DeleteThayDoiChucVuReduceBO(int _ThayDoiChucVuID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_ThayDoiChucVuID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int ThayDoiChucVuID;
            public bool directDelete = false;
            public RootCriteria(int _ThayDoiChucVuID)
            {
                this.ThayDoiChucVuID = _ThayDoiChucVuID;
                this.directDelete = false;
            }

            public RootCriteria(int _ThayDoiChucVuID, bool directDelete)
            {
                this.ThayDoiChucVuID = _ThayDoiChucVuID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int ThayDoiChucVuID;
            public bool directDelete = false;
            public ChildCriteria(int _ThayDoiChucVuID)
            {
                this.ThayDoiChucVuID = _ThayDoiChucVuID;
                this.directDelete = false;
            }

            public ChildCriteria(int _ThayDoiChucVuID, bool directDelete)
            {
                this.ThayDoiChucVuID = _ThayDoiChucVuID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThayDoiChucVuReduceBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[ThayDoiChucVuID] = " + criteria.ThayDoiChucVuID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThayDoiChucVuReduceBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["ThayDoiChucVuID"] is DBNull))
                this._ThayDoiChucVuID = (int)dataReader["ThayDoiChucVuID"];
            if (!(dataReader["NhanVienID"] is DBNull))
                this._NhanVienID = (int)dataReader["NhanVienID"];
            if (!(dataReader["PhongBanID"] is DBNull))
                this._PhongBanID = (int)dataReader["PhongBanID"];
            if (!(dataReader["ChucVuID"] is DBNull))
                this._ChucVuID = (int)dataReader["ChucVuID"];
            if (!(dataReader["NgayApDung"] is DBNull))
                this._NgayApDung = (DateTime)dataReader["NgayApDung"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_ThayDoiChucVuID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_ThayDoiChucVuID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
